﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStuctureStudy.Recursives
{
    class TailRecursiveTest
    {
        public static void Test()
        {
            Console.WriteLine(Fac(10, 1));
        }

        private static int TailRecursiveFac(int n, int accumulator)
        {
            if (n == 0)
            {
                return accumulator;
            }

            return Fac(n - 1, n * accumulator);
        }

        private static int Fac(int n, int accumulator)
        {
            while (true)
            {
                var tempN = n;
                var tempAccumulator = accumulator;

                if (tempN == 0)
                {
                    return tempAccumulator;
                }

                n = tempN - 1;
                accumulator = tempN * tempAccumulator;
            }
        }
    }
}
